﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Checkbox
{
    public partial class Form1 : Form
    {
        //获取图片路径 随机更换大头像 学习time控件和图片的使用
        String[] path = Directory.GetFiles(@"D:\Note12Tpro\Pictures", ".jpg");
        //设置变量让图片自动转换    
        int i = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //i++;
            //if (i == path.Length) i = 0;
            //else {
            //     pictureBox1.Image = Image.FromFile(path[i]);
            //}
        }
        /// <summary>
        /// 初始化加载年份加载图片路径以及音乐路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            //窗体加载时候给一个路径显示图片方法
            pictureBox1.Image = Image.FromFile(@"D:\Note12Tpro\Pictures\5bb729903f9d8675179abd8d284b4a0acf1a543f47e625aae14820dada9b0d04.0.GIF");
            ///给生日附上初始值
            int nowYear = DateTime.Now.Year;
            int notMonth = DateTime.Now.Month;
            int day = DateTime.Now.Day;
            for (int i = 1920; i <= nowYear; i++) {
                YearcomboBox.Items.Add(i + "年");
            }
            String[] imagePath = Directory.GetFiles(@"D:\Note12Tpro\Pictures");
            for (int i =0;i<imagePath.Length;i++) {
                PicturelistBox.Items.Add(imagePath[i]);
            }
            String[] musicPath = Directory.GetFiles(@"E:\音乐下载");
            for (int i = 0; i < musicPath.Length; i++) {
                MusiclistBox.Items.Add(musicPath[i]);
            }
        }
        String hobby = "";
        String ShenFen = "";
        String gender = "";
        /// <summary>
        /// 保存填写信息为txt文本
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveInput_Click(object sender, EventArgs e)
        {
            String hobby = "";
            if (checkBox1.Checked) hobby += checkBox1.Text;
            if (checkBox2.Checked) hobby += checkBox2.Text;
            if (checkBox3.Checked) hobby += checkBox3.Text;
            if (checkBox4.Checked) hobby += checkBox4.Text;
            String ShenFen = "";
            if (studentButton.Checked) ShenFen += studentButton.Text;
            else if (TeacherButton.Checked) ShenFen += TeacherButton.Text;
            String gender = "";
            if (GirlradioButton.Checked) gender += GirlradioButton.Text;
            else if (BoyradioButton.Checked) gender += BoyradioButton.Text;
            //遍历每一个框架 然后is确定属性 as转化为当前属性 加入值
            //foreach (Control cl in this.Controls)
            //{
            //    if (cl is CheckBox)
            //    {
            //        CheckBox cb = cl as CheckBox;
            //        if (cb.Checked) hobby += cb.Text + ",";
            //    }
            //}
            label8.Text = hobby + "," + ShenFen + "," + gender;
            String Info = "个人信息：\r\n" + textBox1.Text + "\r\n" + textBox2.Text + "\r\n" + YearcomboBox.Text + MonthcomboBox.Text  + DaycomboBox.Text + "\r\n" + label8.Text;
            using (FileStream fs =new FileStream(@"D:\C#project\Checkbox写入本地记事本显示不全BUG\mytxt.txt", FileMode.OpenOrCreate,FileAccess.Write)) {
                byte[] buffer = System.Text.Encoding.Default.GetBytes(Info);
                fs.Write(buffer,0, buffer.Length);
                MessageBox.Show("保存成功");
            }
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 当年份发生变化时候更新月份
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void YearcomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            MonthcomboBox.Items.Clear();
            MonthcomboBox.Text = "";
            DaycomboBox.Items.Clear();
            DaycomboBox.Text = "";
            for (int i = 1; i <= 12; i++)
            {
                MonthcomboBox.Items.Add(i + "月");
            }

        }
        /// <summary>
        /// 当月份发生变化判断年份是否为闰年和是否为2月份以及小月和大月
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MonthcomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            DaycomboBox.Items.Clear();
            int year = Convert.ToInt32(YearcomboBox.SelectedItem.ToString().Split(new char[] { '年' }, StringSplitOptions.RemoveEmptyEntries)[0]);
            int month = Convert.ToInt32(MonthcomboBox.SelectedItem.ToString().Split(new char[] { '月' }, StringSplitOptions.RemoveEmptyEntries)[0]);

            switch (month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    for (int i = 1; i <= 31; i++)
                    {
                        DaycomboBox.Items.Add(i + "日");
                    }
                    break;
                case 2:
                    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                    {
                        for (int i = 1; i <= 29; i++)
                        {
                            DaycomboBox.Items.Add(i + "日");
                        }
                    }
                    else{
                        for (int i = 1; i <= 28; i++)
                        {
                            DaycomboBox.Items.Add(i + "日");
                        }
                    }
                    break;
                default:
                    for (int i = 1; i <= 30; i++)
                    {
                        DaycomboBox.Items.Add(i + "日");
                    }
                    break;
            }
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
           String url= "https://" + UrltextBox.Text;
            System.Uri uri = new Uri(url);
            webBrowser1.Url = uri;
        }

        private void PicturelistBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            pictureBox2.Image = Image.FromFile((String) PicturelistBox.Items[PicturelistBox.SelectedIndex]);
        }

        private void MusiclistBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            SoundPlayer sp = new SoundPlayer();
            sp.SoundLocation = @"E:\音乐下载\DR.L - 跑跑卡丁车圣诞主题 (DR.L Bootleg).mp3";
            //sp.SoundLocation =(String)MusiclistBox.Items[MusiclistBox.SelectedIndex];
            sp.Load();
            sp.Play();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Playerlabel.Text = button1.Text;
            NewMethod(button1.Text);

        }
        /// <summary>
        /// 判断谁赢谁输
        /// </summary>
        private void NewMethod(String Fist)
        {
            Player py = new Player();
            int playerFen = py.InputFen(Fist);
            Computer cp = new Computer();
            int cpFen = cp.InputFen();
            AIlabel.Text = cp.Fist;
            Caipan caipan = new Caipan();
            label7.Text = caipan.printResult(playerFen, cpFen).ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Playerlabel.Text = button2.Text;
            NewMethod(button2.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Playerlabel.Text = button3.Text;
            NewMethod(button3.Text);
        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            hobby += cb.Text;
        }
    }
}
